package cn.stockeye.client;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TextArea;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * 
 * 创建闪动的托盘图像
 * 
 * @author Everest
 * 
 */
public class BickerTray extends JFrame implements Runnable {


	private static final long serialVersionUID = -3115128552716619277L;

	private SystemTray sysTray;// 当前操作系统的托盘对象
	private TrayIcon trayIcon;// 当前对象的托盘

	private ImageIcon icon = null;
	private TextArea ta = null;
	HttpService http = new HttpService();

	private long count = 1; // 记录消息闪动的次数
	private boolean flag = false; // 是否有新消息
	private boolean hasClicked = false; // 是否已经点击

	public void start() {
		this.createTrayIcon();// 创建托盘对象
		Image image = this.getToolkit().getImage(
				getRes("cn/stockeye/client/f32.gif"));
		this.setIconImage(image);
		init();
	}

	private URL getRes(String str) {
		return this.getClass().getClassLoader().getResource(str);
	}

	/**
	 * 初始化窗体的方法
	 */
	private void init() {
		this.setTitle("消息盒子");
		ta = new TextArea("");
		ta.setEditable(false);
		this.add(ta);
		this.setSize(400, 400);
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		// 添加窗口最小化事件,将托盘添加到操作系统的托盘
		/*
		 * this.addWindowListener(new WindowAdapter() { public void
		 * windowIconified(WindowEvent e) { addTrayIcon(); } });
		 */
		addTrayIcon();
		this.setVisible(false);
		// setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				ta.setText("");
				hasClicked = false;
				count = 0;
			}
		});
	}

	/**
	 * 添加托盘的方法
	 */
	private void addTrayIcon() {
		try {
			sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘
			setVisible(false); // 使得当前的窗口隐藏
			new Thread(this).start();
			new Thread(new Monitor()).start();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	class Monitor implements Runnable{

		@Override
		public void run() {
			while(true){
				String msg = http.getResponse("http://www.stockeye.cn/infos");
				setMessage(msg);
				if (ta.getText() != null && !ta.getText().equals("")) {
					flag = true;
					count=0;
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	} 

	/**
	 * 创建系统托盘的对象 步骤: 1,获得当前操作系统的托盘对象 2,创建弹出菜单popupMenu 3,创建托盘图标icon
	 * 4,创建系统的托盘对象trayIcon
	 */
	private void createTrayIcon() {
		sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象
		icon = new ImageIcon(
				getRes("cn/stockeye/client/f17.gif"));// 托盘图标
		PopupMenu popupMenu = new PopupMenu();// 弹出菜单
		MenuItem open = new MenuItem("open");
		MenuItem exit = new MenuItem("close");
		popupMenu.add(open);
		popupMenu.add(exit);
		// 为弹出菜单项添加事件
		/*
		 * open.addActionListener(new ActionListener() { public void
		 * actionPerformed(ActionEvent e) { ta.setText(ta.getText() +
		 * "\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"
		 * + times + "次接收时间：" + new Date().toLocaleString()); // 设置通知消息内容
		 * BickerTray.this.setExtendedState(JFrame.NORMAL);
		 * BickerTray.this.setVisible(true); // 显示窗口 BickerTray.this.toFront();
		 * // 显示窗口到最前端 flag = false; // 消息打开了 count = 0; times++; } });
		 * exit.addActionListener(new ActionListener() { public void
		 * actionPerformed(ActionEvent e) { System.exit(0); } });
		 */
		trayIcon = new TrayIcon(icon.getImage(), "消息盒子", popupMenu);
		/** 添加鼠标监听器，当鼠标在托盘图标上双击时，默认显示窗口 */
		trayIcon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) { // 鼠标双击
					ta.setText(ta.getText()); // 设置通知消息内容
					BickerTray.this.setExtendedState(JFrame.NORMAL);
					BickerTray.this.setVisible(true); // 显示窗口
					BickerTray.this.toFront();
					flag = false; // 消息打开了
					hasClicked = true;
				}
			}
		});
	}

	/**
	 * 线程控制闪动
	 */
	public void run() {
		
		while (true) {
			if (flag) { // 有新消息
				try {
					if (count == 1) {
						// 播放消息提示音
						AudioClip p = Applet
								.newAudioClip(getRes("cn/stockeye/client/msg.wav"));
						p.play();
					}
					if (!hasClicked) {
						// 闪动消息的空白时间
						this.trayIcon.setImage(new ImageIcon("").getImage());
						Thread.sleep(500);
						// 闪动消息的提示图片
						this.trayIcon.setImage(icon.getImage());
						Thread.sleep(500);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				count++;
			}else{
				this.trayIcon.setImage(icon.getImage());
			}
		}
	}

	public void setMessage(String message) {
		if(message == null || message.trim().equals("")){
			return ;
		}
		StringBuilder sb = new StringBuilder(ta.getText());
		sb.append('\n').append(message);
		ta.setText(sb.toString());
	}

	public static void main(String[] args) {

		JFrame.setDefaultLookAndFeelDecorated(false);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new BickerTray().start();
			}
		});

	}
}
